package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.ClientPlant;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.ClientMapper;
import cn.wolfcode.crm.mapper.ClientPlantMapper;
import cn.wolfcode.crm.query.ClientPlantQueryObject;
import cn.wolfcode.crm.service.IClientPlantService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ClientPlantServiceImpl implements IClientPlantService {
    @Autowired
    private ClientPlantMapper mapper;
    @Autowired
    private ClientMapper clientMapper;


    public void deleteByPrimaryKey(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public void insert(ClientPlant record) {
        mapper.insert(record);
    }

    public ClientPlant selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<ClientPlant> selectAll() {
        return mapper.selectAll();
    }

    public void updateByPrimaryKey(ClientPlant record) {
        mapper.updateByPrimaryKey(record);
    }

    public PageResult query(ClientPlantQueryObject qo) {
        int count = mapper.queryForCount(qo);
        if (count == 0) {
            return new PageResult(0, null);
        }
        List<ClientPlant> list = mapper.queryForList(qo);
        return new PageResult(count, list);
    }

    public void positive(Long id) {
        //先根据id查出表中的用户名 等信息
        Client client = mapper.insertClientById(id);
        //将  state设置为 1   developsgin设置为null 时间和操作人 也需要修改
        client.setState(Client.STATE_OFFI);
        client.setDevelopsgin(null);
        client.setInputdate(new Date());
        //当前用户id设置为3L
        Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        client.setInputuser(currentUser);
        //更具这个客户的id 去更改 信息
        clientMapper.updateClientPositiveFromPlan(client);
        //删除开发表中的信息
        mapper.deleteByPrimaryKey(id);
    }

    //转到资源池
    public void planmovesource(Long id) {
        Client client = mapper.insertClientById(id);
        //将  state设置为 1   developsgin设置为null 时间和操作人 也需要修改
        client.setState(Client.STATE_SOURCE);
        client.setDevelopsgin(null);
        client.setInputdate(new Date());
        //当前用户id设置为3L
        Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        client.setInputuser(currentUser);
        //更具这个客户的id 去更改 信息
        clientMapper.updateClientPositiveFromPlan(client);
        //删除开发表中的信息
        mapper.deleteByPrimaryKey(id);
    }

    public void moveInSource(ClientPlant cp) {
        //修改当前操作时间
        cp.setCreattime(new Date());
        //修改当前操作人
        Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        cp.setOperator(currentUser);
        mapper.updateByPrimaryKey(cp);
    }
}
